# covariate balance tests

## load OLD ESS 10 data with missing values (new data set does not contain missings) ##
load("ESS10old.RData")


## recode old data ##
ESS10old <- tidyr::separate(ESS10old, inwds, c("date", "time"), sep = " ")
ESS10old$date <- as.Date(ESS10old$date)
ESS10old$date <- as.numeric(ESS10old$date)
ESS10old$date <- ESS10old$date - 18854 # 15 Aug 2021 is 0 now (Taliban Takeover) 



# mig 
ESS10old$mig <- as.numeric(ESS10old$imwbcnt) 
ESS10old$mig[ESS10old$mig == 77 | ESS10old$mig == 88 | ESS10old$mig == 99] <- NA

# eisced 
ESS10old$eisced[ESS10old$eisced == 55 | ESS10old$eisced == 77 | ESS10old$eisced == 88 | ESS10old$eisced == 99] <- NA
ESS10old$eisced <- as.numeric(ESS10old$eisced)

# wkhtot
ESS10old$wkhtot[ESS10old$wkhtot == 666 | ESS10old$wkhtot == 777 | ESS10old$wkhtot == 888 | ESS10old$wkhtot == 999] <- NA
ESS10old$wkhtot <- as.numeric(ESS10old$wkhtot)


test <- ESS10old %>% 
  filter(date > -15 & date < 15)

education <- rdd_data(test$mig, test$date, cutpoint = 0, covar = test$eisced)
covarTest_mean(education)

age <- rdd_data(test$mig, test$date, cutpoint = 0, covar = test$agea)
covarTest_mean(age)

gender <- rdd_data(test$mig, test$date, cutpoint = 0, covar = test$gndr)
covarTest_mean(gender)

workhours <- rdd_data(test$mig, test$date, cutpoint = 0, covar = test$wkhtot)
covarTest_mean(workhours)

#Item non-response / refusal balance tests
test <- test %>% 
  mutate(NRmig = ifelse(imwbcnt == 77,1,0))

test <- test %>% 
  mutate(NReco = ifelse(imbgeco == 77,1,0))

test <- test %>% 
  mutate(NRcul = ifelse(imueclt == 77,1,0))

NR1 <- rdd_data(test$mig, test$date, cutpoint = 0, covar = test$NRmig)
covarTest_mean(NR1)

NR2 <- rdd_data(test$mig, test$date, cutpoint = 0, covar = test$NReco)
covarTest_mean(NR2)

NR3 <- rdd_data(test$mig, test$date, cutpoint = 0, covar = test$NRcul)
covarTest_mean(NR3)

